package com.utc.teamcenter.common.app.dialog.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JPanel;

import com.teamcenter.rac.util.PropertyLayout;
import com.utc.teamcenter.common.util.EclipseUtil;

public class BeanPanel {

	private List<BeanComponent> componentList = new ArrayList<BeanComponent>();
	private Map<String, BeanComponent> componentMap = new HashMap<String, BeanComponent>();

	private JPanel panel = new JPanel(new PropertyLayout());
	private int index = 1;

	public BeanPanel() {
		super();
	}

	public List<BeanComponent> getBeanComponents() {
		return componentList;
	}

	public Map<String, BeanComponent> getComponentMap() {
		return componentMap;
	}

	public void add(BeanComponent beanComponent) {
		if (beanComponent != null) {
			this.componentList.add(beanComponent);
			this.componentMap.put(beanComponent.getKey(), beanComponent);

			beanComponent.addBeanToPanel(panel, index++);
		}
	}

	public void remove(BeanComponent beanComponent) {
		if (beanComponent != null) {
			this.componentList.remove(beanComponent);
			this.componentMap.remove(beanComponent.getKey());

			this.index--;
			this.panel.remove(beanComponent.getComponent());
			this.panel.remove(beanComponent.getLabel());
		}
	}

	public BeanComponent getBeanComponent(String key) {
		return this.componentMap.get(key);
	}

	public JComponent getJComponent(String key) {
		BeanComponent beanComponent = this.componentMap.get(key);

		if (beanComponent != null) {
			return beanComponent.getComponent();
		}

		return null;
	}

	public String getBeanComponentText(String key) {
		BeanComponent beanComponent = getBeanComponent(key);

		String text = "";
		if (beanComponent != null) {
			text = beanComponent.getText();
		}

		return text;
	}

	public void addBeanComponentToPanel(JPanel panel, int i) {
		int index = i;

		for (BeanComponent beanComponent : this.componentList) {
			beanComponent.addBeanToPanel(panel, index++);
		}
	}

	public void clear() {
		for (BeanComponent beanComponent : this.componentList) {
			beanComponent.clear();
		}
	}

	public void setEnabled(boolean bool) {
		for (BeanComponent beanComponent : this.componentList) {
			beanComponent.setEnabled(bool);
		}
	}

	public void setVisible(boolean bool) {
		for (BeanComponent beanComponent : this.componentList) {
			beanComponent.setVisible(bool);
		}
	}

	public boolean isReady() {
		for (BeanComponent beanComponent : this.componentList) {
			String value = beanComponent.getText();

			if (value == null || "".equals(value)) {
				if (beanComponent.isRequired()) {
					EclipseUtil.info(beanComponent.getName() + " is Required.");
					beanComponent.requestFocus();

					return false;
				}
			}
		}

		return true;
	}

	@Override
	public String toString() {
		return "BeanListMap [componentList=" + componentList + "]";
	}
}
